<?php

function getCurrentRoundStatus() {
	$round = getCurrentRound();
	
	$result = mysql_query("SELECT status from round where round = $round");
	$row = mysql_fetch_array($result);
	$currentRoundStatus = $row['status'];
	
	
	return $currentRoundStatus;
}

function getPartialLockoutTeams() {
	$round = getCurrentRound();
	
	$result = mysql_query("SELECT partialLockoutTeams from round where round = $round");
	$row = mysql_fetch_array($result);
	$partialLockoutTeams = $row['partialLockoutTeams'];
	
	
	return preg_split("%,%", $partialLockoutTeams);
}

function getCurrentRound() {
	$result = mysql_query("SELECT round as currentRound from round where status in ('" . RoundStatusType::Open . "', '" . RoundStatusType::Partial_Lockout . "', '" . RoundStatusType::Lockout . "')");
	
	$row = mysql_fetch_array($result);
	$currentRound = $row['currentRound'];
	if ($currentRound == null) {
		$currentRound = 1;
	}
	
	
	return $currentRound;
}

function getRound($roundId, $year, $isArchive) {
	$sql = "select id from round" . getTableExtension($isArchive) . " where round = $roundId and year = $year;";
	$result = mysql_query($sql);
	
	$row = mysql_fetch_array($result);
	$roundId = $row['id'];
	
	
	return $roundId;
}

function updateRoundStatus($hours) {
	$currentRound = getCurrentRound();
	$sql = "SELECT status, partialLockoutDateTime, lockoutDateTime from round where round = $currentRound";
	$result = mysql_query($sql);
	$now = strtotime("+" . $hours . " hours");
	
	$row = mysql_fetch_array($result);
	$partialLockoutDateTime = $row['partialLockoutDateTime'];
	$lockoutDateTime = $row['lockoutDateTime'];
	$status = $row['status'];
		
	if ($status == RoundStatusType::Open) {
		if ($partialLockoutDateTime != null) {
			if ($now > strtotime($partialLockoutDateTime)) {
				$sql = "UPDATE round set status = '" . RoundStatusType::Partial_Lockout . "' where round = $currentRound";
				mysql_query($sql);
			}
		}
		else if ($now > strtotime($lockoutDateTime)) {
			$sql = "UPDATE round set status = '" . RoundStatusType::Lockout . "' where round = $currentRound";
			mysql_query($sql);
		}
	}
	else if ($status == RoundStatusType::Partial_Lockout && $now > strtotime($lockoutDateTime)) {
		$sql = "UPDATE round set status = '" . RoundStatusType::Lockout . "' where round = $currentRound";
		mysql_query($sql);		
	}
}

function updateRoundTables() {
	$sql = "UPDATE team set teamSubmittedStatus = '" . TeamSubmittedStatus::Not_Submitted . "'";
	mysql_query($sql);
	
	$sql = "UPDATE team set tradesForRound = 0";
	mysql_query($sql);	
	
	$sql = "UPDATE timetoperformtask set done = 0;";
	mysql_query($sql);
	
	$round = getCurrentRound();
	
	$sql = "UPDATE round set status = 'COMPLETE' where round = $round;";
	mysql_query($sql);
	
	$sql = "UPDATE settings SET value = '1' WHERE name = 'RESULTS_PAGE_ID' or name = 'SUBMITTED_TEAMS_PAGE_ID'";
	mysql_query($sql);
	
	$sql = "UPDATE round set status = '" . RoundStatusType::Open . "' where round = " . ($round + 1);
	mysql_query($sql);
	
	$selectedTeamsReleaseDays = getSelectedTeamsReleaseDays($round);
	if ($selectedTeamsReleaseDays != "BOTH") {
		$sql = "UPDATE timetoperformtask set done = 1 where task = '" . TimeToPerformTaskType::SendReminders . "' and day = '4';";
		mysql_query($sql);
		$sql = "UPDATE timetoperformtask set done = 1 where task = '" . TimeToPerformTaskType::UploadSelectedTeams . "' and day = '3';";
		mysql_query($sql);
		$sql = "UPDATE timetoperformtask set done = 1 where task = '" . TimeToPerformTaskType::ScoreCheck . "' and orderVal <= 9;";
    	mysql_query($sql);
	}
		
	
}

function getAFLSiteRoundId() {
    $round = getCurrentRound();
	$sql = "SELECT aflSiteRoundId from round where round = $round";
	$result = mysql_query($sql);
	
	$row = mysql_fetch_array($result);
	$aflSiteRoundId = $row['aflSiteRoundId'];
	
	return $aflSiteRoundId;
}

function getSelectedTeamsReleaseDays($round) {
	$sql = "SELECT selectedTeamsReleaseDays FROM round where round = $round";
	
	$result = mysql_query($sql);
	
	$row = mysql_fetch_array($result);
	$selectedTeamsReleaseDays = $row['selectedTeamsReleaseDays'];

	return $selectedTeamsReleaseDays;
}

function setRoundProperty($property, $value, $round) {
	$sql = "UPDATE round set $property = '$value' WHERE round = $round";
	mysql_query($sql);
}

function getRoundProperty($property, $round) {
	$sql = "SELECT selectedTeamsReleaseDays, $property as property FROM round where round = $round";
	$result = mysql_query($sql);
	
	$row = mysql_fetch_array($result);
	$roundDetails = new RoundDetails();
	$roundDetails->property = $row['property'];
	$roundDetails->selectedTeamsReleaseDays = $row['selectedTeamsReleaseDays'];

        return $roundDetails;
}

?>